package tump;

import tools.DateStrTanslationUnits;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @Author: sun
 * @Description:八叉树的数据结构
 * @Data: create in 15:49 2019/1/27
 * @Modfied by:
 */
public class Octree {

    /**
     * 文件URL
     */
    String url_1 = "F:\\PycharmProjects\\GraduationProject\\src\\main\\resources\\DATA\\tump.txt";
    String url_2 = "H:\\我的坚果云\\毕业设计\\src\\main\\resources\\DATA\\tump.txt";
    private final String FILEURL = url_1;


    /**
     * 根节点,level:0 id:0 子节点的level: level+1,id: 8*(level)+i i属于1到7
     */
    private OctreeNode root = null;

    public Octree() {
        root = new OctreeNode(0, 0);
    }

    public void add(Long id, String data) {

    }

    /**
     * 以行为单位读取文件，常用于读面向行的格式化文件
     */
    public static void readFileByLines(String fileName, OctreeNode root) throws ParseException {
        File file = new File(fileName);
        BufferedReader reader = null;

        double startTime = DateStrTanslationUnits.dateToDouble("2016.12.01-00:00:00");
        double endTime = DateStrTanslationUnits.dateToDouble("2016.12.31-24:00:00");
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                String[] dates = tempString.split(",");
                String day = dates[1].substring(dates[1].length() - 2);
                double time = DateStrTanslationUnits.dateToDouble("2016.12." + day + "-00:00:00");
                TimeGeoHashUtil timeGeoHashUtil = new TimeGeoHashUtil(Double.valueOf(dates[9]), Double.valueOf(dates[8]), time, startTime, endTime);

                byte[] bArr = Base32Util.decode(timeGeoHashUtil.getGeoHashBase32());
                ByteBuffer buffer = ByteBuffer.allocate(8);
                buffer.put(bArr, 0, bArr.length);
                buffer.flip();//need flip

                //# region 显示提示信息
                Long myLong = buffer.getLong() >>> 4;
                Long level_0_ID = myLong >>> (60 - (3 * 0));
                Long level_1_ID = myLong >>> (60 - (3 * 1));
                Long level_2_ID = myLong >>> (60 - (3 * 2));
                System.out.println("level:" + level_0_ID + "-" + level_1_ID + "-" + level_2_ID + "\tmylong:" + myLong + "\tline " + line + ": " + day + " , " + dates[9] + " , " + dates[8] + " , " + timeGeoHashUtil.getGeoHashBase32());
                //# endregion

                root.add(myLong);
                line++;
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    public void read(String url, OctreeNode root) throws ParseException {
        readFileByLines(url, root);
    }

    /**
     * @param startTime
     * @param endTime
     * @param lon1
     * @param lat1
     * @param lon2
     * @param lat2
     */
    private List<String> findRange(double startTime, double endTime, double lon1, double lat1, double lon2, double lat2) throws ParseException {

        double MINLAT = -90;
        double MAXLAT = 90;
        double MINLNG = -180;
        double MAXLNG = 180;
        double minTime = DateStrTanslationUnits.dateToLong("2016.12.01-00:00:00");
        double maxTime = DateStrTanslationUnits.dateToLong("2016.12.31-24:00:00");

        double helf = (minTime + maxTime) / 2;
        if (startTime < helf && endTime < helf) {
            System.out.println(" 0 ");
        }else if(startTime<helf&&endTime>helf){
            System.out.println(" 0 1 ");
        }else {
            System.out.println(" 1 ");
        }




        return null;
    }


    public static void main(String[] args) {
        Octree octree = new Octree();
        OctreeNode root = new OctreeNode(0, 0);

        try {
            octree.read(octree.FILEURL, root);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(root);
    }
}
